<template>
	<view class="container">
		<form @submit="requestUpdatepass" @reset="">
			<view class="updatepass-before" v-if="showBeforeOrnext=='before'">
				<view class="phone">
					<text>发送验证码到您的手机号 {{userInfo.phone}}</text>
				</view>
				<view class="code_input">
					<input type="text" value="" placeholder="输入验证码" v-model="inputcode"/>
					<text @tap="sendCode()" v-if="allowSendcode" class="sendtext">发送验证码</text>
					<text v-else class="awaittext">{{awaitText}}</text>
				</view>
				<view class="next_step" @tap="getNextstep()">
					<text>下一步</text>
				</view>
			</view>
			<view class="updatepass-next" v-if="showBeforeOrnext=='next'">
				<view class="newpass">
					<input type="password" value="" placeholder="输入新密码" v-model="newpass"/>
				</view>
				<view class="newpasssec">
					<input type="password" value="" placeholder="再次输入新密码" v-model="secpass"/>
				</view>
				<button form-type="submit">完成</button>
			</view>
		</form>
	</view>
</template>

<script>
	import {sendCode} from '@/apis/request.js'
	import {updatePassLogined} from  '@/apis/request.js'
	export default {
		data() {
			return {
				showBeforeOrnext:'before',
				userInfo:{},
				code:'',
			    allowSendcode:true,
				awaitText:'',
				inputcode:'',
				newpass:'',
				secpass:''
			}
		},
		onLoad() {
			this.userInfo=uni.getStorageSync('userInfo');
		},
		methods: {
			//发起修改密码请求
			requestUpdatepass(){
				if(this.newpass == ''){
					uni.showToast({
						icon:'none',
						title:'输入新密码'
					});
					return ;
				}
				if(this.secpass == ''){
					uni.showToast({
						icon:'none',
						title:'输入确认新密码'
					});
					return ;
				}
				if(this.newpass != this.secpass){
					uni.showToast({
						icon:'none',
						title:'两次密码输入不一致'
					});
					return ;
				}
				this.$http({
					url:updatePassLogined,
					method:"POST",
					data:{
						newPassword:this.newpass,
						phone:this.userInfo.phone,
						vcode:this.code
					},
					header:{
						'content-type':'application/x-www-form-urlencoded',
						'Token':this.userInfo.token
					}
				})
				.then(res=>{
					if(res[1].data.status=='200'){
						uni.showToast({
							title:'已修改'
						});
						setTimeout(()=>{
							uni.reLaunch({
								url:'/pages/login/login'
							})
						},500);
					}
				})
				.catch(err=>{
					console.log(err);
				});
			},
			//验证下一步
			getNextstep(){
				if(this.inputcode==''){
					uni.showToast({
						icon:'none',
						title:'输入验证码'
					});
					return ;
				}
				if(this.inputcode != this.code){
					uni.showToast({
						icon:'none',
						title:'验证码错误'
					});
					return ;
				}
				this.showBeforeOrnext='next';
			},
			//发送验证码
			sendCode(){
				this.$http({
					url:sendCode,
					method:"GET",
					data:{
						phone:this.userInfo.phone
					},
					header:{
						'content-type':'application/x-www-form-urlencoded',
						'Token':this.userInfo.token
					}
				})
				.then(res=>{
					// console.log(res);
					if(res[1].data.status == '200')
					{
						//发送成功
						this.code=res[1].data.data;
						this.allowSendcode=false;
						let seconds=60;
						this.awaitText=`${seconds}s后可发送`;
						let timer=setInterval(()=>{
							--seconds;
							this.awaitText=`${seconds}s后可发送`;
						},1000);
						setTimeout(()=>{
							this.allowSendcode=true;
							clearInterval(timer);
						},60000);
					}
				})
				.catch(err=>{
					console.log(err);
				});
			}
		}
	}
</script>

<style lang="scss">
    page{
		width: 100%;
		height: 100%;
	}
	.container{
		width: 100%;
		height: 100%;
		.updatepass-before{
			width: 100%;
			height: auto;
			view{
			   display: flex;
			   justify-content: space-between;
			}
			.phone{
				width: 100%;
				height: 100rpx;
				border-bottom: 1rpx solid #F1F1F1;
				line-height: 100rpx;
				text{font-size: 28rpx; color:#888;margin-left: 20rpx;}
			}
			.code_input{
				width: 100%;
				height: 100rpx;
				border-bottom: 1rpx solid #F1F1F1;
				line-height: 100rpx;
				input{
					margin-left: 20rpx;
					font-size: 28rpx;
					width: 250rpx;
					height: 100rpx;
				}
				text{margin-right: 20rpx;font-size: 25rpx;}
				.sendtext{
					color: #222;
				}
				.awaittext{
				    color: #888;
				}
			}
			.next_step{
			   color: #FFFFFF;
			   font-size: 28rpx;
			   width:650rpx;
			   height: 90rpx;
			   display: flex;
			   justify-content: center;
			   align-items: center;
			   background-color: #18B566;
			   border-radius: 10rpx;
			   margin: auto;
			   margin-top: 200rpx;
			}
		}
		.updatepass-next{
			width: 100%;
			height: auto;
			view{
				input{
					width: 710rpx;
					height: 100rpx;
					margin: auto;
					font-size: 28rpx;
				}
			}
			.newpass{
				width: 100%;
				height: 100rpx;
				border-bottom: 1rpx solid #F1F1F1;
				line-height: 100rpx;
			}
			.newpasssec{
				width: 100%;
				height: 100rpx;
				border-bottom: 1rpx solid #F1F1F1;
				line-height: 100rpx;
			}
			button{width: 650rpx;height: 90rpx;background-color: #18B566;color: #FFFFFF;border-radius: 10rpx;margin: auto;margin-top:200rpx;display: flex;align-items: center;justify-content: center;}
		}
	}
</style>
